<?php

/* 
 * Manager/TransfertManager.php
 */
namespace RHX\Model\Manager;

use Zend\Db\Adapter\Adapter;

class TransfertManager{
    protected $adapter = null;
    
    /**
     * Constructeur
     * 
     * @param \Zend\Db\Adapter\Adapter $adapter
     */
    public function __construct(Adapter $adapter) {
	$this->adapter= $adapter;
    }
    
    /**
     * Adapteur de base de données
     * 
     * @return Adapter
     */
    public function getAdapter(){
	return $this->adapter;
    }
    
    /**
     * Détermine si un transfert est valable ou non
     * 
     * @param int $matricule
     * @param string $date
     * @param string $entree
     * @param string $sortie
     * @return array
     */
    public function isAllowed($matricule, $date, $entree, $sortie, $transfertId = null){
	$transfertId = (is_null($transfertId)) ? 0 : (int)$transfertId;
	$sql = "SELECT	* FROM rh_transfert "
		. "WHERE jour = '$date' "
		. "AND (entree BETWEEN '$entree' AND '$sortie' OR sortie BETWEEN '$entree' AND '$sortie') "
		. "AND matricule = $matricule ";
	$result = $this->getAdapter()
		->query($sql)
		->execute();
	$message = $this->buildMessage($result);
	$allowed = false;
	if($transfertId != 0){
	    $data = $this->verifyHour($matricule, $date, $entree, $sortie, $transfertId);
	    $message = $data['message'];
	    $allowed = $data['allowed'];
	}else{
	    $allowed = ($result->count() > 0) ? false : true;
	}
	return array(
	    'allowed' => $allowed,
	    'message' => $message,
	);
    }
    
    /**
     * Vérification des horaires si il exite un autre transfert ayant les mêmes plages horaires
     * 
     * @param int $matricule
     * @param string $date
     * @param string $entree
     * @param string $sortie
     * @param int $transfertId
     * @return boolean
     */
    public function verifyHour($matricule, $date, $entree, $sortie, $transfertId){
	$sql = "SELECT	* FROM rh_transfert "
		. "WHERE jour = '$date' "
		. "AND (entree BETWEEN '$entree' AND '$sortie' OR sortie BETWEEN '$entree' AND '$sortie') "
		. "AND matricule = $matricule "
		. "AND id != $transfertId";
	$result = $this->getAdapter()
		->query($sql)
		->execute();
	$message = $this->buildMessage($result);
	$allowed = true;
	if($result->count() > 0){
	    $allowed = false;
	}
	return array(
	    'allowed' => $allowed,
	    'message' => $message,
	);
    }
    
    /**
     * Construire le message résumé des transferts
     * 
     * @param type $result
     * @return string
     */
    public function buildMessage($result){
	$message = '<table class="table table-striped table-condensed list-table">';
	$message .= '<thead><tr><td>Mat.</td><td>Nom</td><td>Départ</td><td>Arrivée</td><td>Jour</td><td>Entrée</td><td>Sortie</td></tr></thead>';
	$message .= '<tbody>';
	foreach($result as $row){
	    $transfert = new \RHX\Model\Object\Transfert($this->getAdapter(), $row);
	    $employe = $transfert->getEmploye();
	    $depart = $transfert->getSiteDepart();
	    $arrivee = $transfert->getSiteArrivee();
	    $jour = new \RHX\Helper\RHDateTime($transfert->journee);
	    $entree = \RHX\Helper\RHDateTime::hourNoSecond($transfert->entree);
	    $sortie = \RHX\Helper\RHDateTime::hourNoSecond($transfert->sortie);
	    $message .= '<tr>';
	    $message .= "<td>$employe->matricule</td><td>$employe->nom1</td><td>$depart->libelle</td><td>$arrivee->libelle</td><td>{$jour->date('d/m/Y')}</td><td>$entree</td><td>$sortie</td>";
	    $message .= '</tr>';
	}
	$message .= '</tbody>';
	$message .= '</table>';
	return $message;
    }
    
    /**
     * Autre transfert du même jour
     * 
     * @param type $transfertId
     * @return \RHX\Model\Object\Transfert
     */
    public function getOtherTransfert($transfertId){
	$transfert = $this->getTransfert($transfertId);
	if(is_null($transfert)){
	    return array();
	}
	$tableTransfert = new \RHX\Model\Table\TransfertTable($this->getAdapter());
	return $tableTransfert->select(function(\Zend\Db\Sql\Select $select)use($transfert){
	    $where = new \Zend\Db\Sql\Where();
	    $where->notEqualTo('id', $transfert->id);
	    $where->equalTo('jour', $transfert->journee);
	    $where->equalTo('matricule', $transfert->matricule);
	    $select->where($where);
	});
    }
    
    /**
     * Transfert
     * 
     * @param type $transfertId
     * @return \RHX\Model\Object\Transfert
     */
    public function getTransfert($transfertId){
	$tableTransfert = new \RHX\Model\Table\TransfertTable($this->getAdapter());
	try{
	    return $tableTransfert->get($transfertId);
	} catch (Exception $ex) {
	    return null;
	}
    }
}

